/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2015
 */

package com.environment_pro.monitor.service.impl;

import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.environment_pro.monitor.mapper.TbWarehouseMapper;
import com.environment_pro.monitor.model.TbWarehouse;
import com.environment_pro.monitor.service.TbWarehouseService;
import com.environment_pro.monitor.util.PageHelper;
import com.environment_pro.monitor.util.PageInfo;
import com.environment_pro.monitor.util.PageHelper.Page;
import com.environment_pro.monitor.util.StaticUtil;

/**
 * @author yangfeng 
 * @version 1.0
 * @since 1.0
 */

@Service
@Transactional  //此处不再进行创建SqlSession和提交事务，都已交由spring去管理了。
public class TbWarehouseServiceImpl implements TbWarehouseService {
	
	@Resource
	private TbWarehouseMapper mapper;

	//移除缓存
	//@CacheEvict(value = "tbWarehouseCache",key="'id'+#id")
	public boolean delete(int id) {
		
		return mapper.delete(id);
	}

	@SuppressWarnings("unchecked")
	public Page<TbWarehouse> findAll(PageInfo pageInfo, TbWarehouse tbWarehouse) {
		PageHelper.startPage(pageInfo);  
		mapper.findAll(tbWarehouse);
		return PageHelper.endPage();
	}

	//使用缓存
	//@Cacheable(value = "tbWarehouseCache",key="'id'+#id")
	public TbWarehouse findById(int id) {
		TbWarehouse tbWarehouse = mapper.findById(id);
		return tbWarehouse;
	}
	
	//使用缓存
	@Cacheable(value = "tbWarehouseCache",key="'code'+#num")
	public TbWarehouse findByNum(String num) {
		TbWarehouse tbWarehouse = mapper.findByNum(num);
		return tbWarehouse;
	}
	
	//缓存数据
	//@CachePut(value = "tbWarehouseCache",key="'id'+#tbWarehouse.id")
	public void save(TbWarehouse tbWarehouse) {
		tbWarehouse.setAddTime(new Date());
		mapper.save(tbWarehouse);
	}
	//更新缓存
	//@CacheEvict(value = "tbWarehouseCache",key="'id'+#tbWarehouse.id")
	public boolean update(TbWarehouse tbWarehouse) {

		return mapper.update(tbWarehouse);
	}

	//编码转化为名称
	public String code2name(String code) {
		TbWarehouse tbWarehouse = (TbWarehouse)StaticUtil.getCacheObject("tbWarehouseCache", String.valueOf("code"+code));
		if (tbWarehouse==null) {
			tbWarehouse = this.findByNum(code);
			StaticUtil.putCacheObject("tbWarehouseCache", String.valueOf("code"+code), tbWarehouse);
		}
		return tbWarehouse.getName();
	}

	//ID转化为名称
	public String id2name(int id) {
		TbWarehouse tbWarehouse = (TbWarehouse)StaticUtil.getCacheObject("tbWarehouseCache", String.valueOf("id"+id));
		if (tbWarehouse==null) {
			tbWarehouse = this.findById(id);
			StaticUtil.putCacheObject("tbWarehouseCache", String.valueOf("id"+id), tbWarehouse);
		}
		return tbWarehouse.getName();
	}

}